/**
 *  *2010-7-26 下午04:41:52  add 
 */
package com.base.myproject.client.HR.holiday;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BaseObject;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 延长年假申请
 * 
 * @author Administrator 2011-08-18 cxghiu4102@gmail.com
 */
public class ProlongYearVacationRequest extends BaseJDBCPanel {
	private Button search = new Button("查询");
	
	public ProlongYearVacationRequest() {
		init();
	}

	public void init() {
		search.addListener(Events.Select, getListener());
		
		initWedgitx();
		registerMainStore(requestStore);
	}
	
	private void initWedgitx() {
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.add(createRequestForm());
		vpanel.add(createRequestGrid());
		
		add(vpanel);
	}
	
	private BaseStore<BusiObject> requestStore = null;

	private BaseStore<BusiObject> getRequestStore() {
		if (requestStore == null) {
			createRequestStore();
		}
		return requestStore;
	}

	private BaseStore<BusiObject> createRequestStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("year");
		type.getField("year").setType(String.class);
		type.addField("requestProlongDate");
		type.getField("requestProlongDate").setType(Date.class);
		type.addField("status");
		type.getField("status").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("prolongYearVacationRequest");
		info.setKeyname(new String[] { "icode"});
		info.setSql("select * from prolongYearVacationRequest");
		info.setForcenolimit("true");
		requestStore = new BaseStore<BusiObject>(type, info);
		
		return requestStore;
	}

	private BaseGridPanel requestGrid = null;
	private DateTimeFormat yearFormat = DateTimeFormat.getFormat("yyyy");
	private DateTimeFormat dateFormat = DateTimeFormat.getFormat("yyyy-MM-dd");

	private BaseGridPanel createRequestGrid() {
		CheckBoxSelectionModel<BaseObject> cbs = new CheckBoxSelectionModel<BaseObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		
		ColumnConfig C1 = new ColumnConfig("bcode", "申请人", 140);
		BaseComboBoxForm<BaseObject> bname = new BaseComboBoxForm<BaseObject>("bcode", "employno %2B bname", "v_bcode2job", "", true);
		C1.setEditor(bname.getCelEditor());
		C1.setRenderer(bname);
		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("year", "申请年度", 80);
		TextField<String> d2 = new TextField<String>();
		C2.setEditor(new CellEditor(d2));
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("requestProlongDate", "延长日期", 120);
		DateField d3 = new DateField();
		d3.getPropertyEditor().setFormat(dateFormat);
		C3.setEditor(new CellEditor(d3));
		C3.setDateTimeFormat(dateFormat);
		columns.add(C3);

		ColumnModel cm = new ColumnModel(columns);
		requestGrid = new BaseGridPanel(getRequestStore(), cm, true, true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		requestGrid.getBg().setSelectionModel(cbs);
		requestGrid.getBg().setHeight(460);
		return requestGrid;
	}

	private static BaseComboBoxForm<BaseObject> bname = new BaseComboBoxForm<BaseObject>("bcode", "employno %2B bname", "v_bcode2job", "", true);
	private DateField startYear = new DateField();
	private DateField endYear = new DateField();
	
	private BaseFormPanel createRequestForm() {
		BaseFormPanel requestForm = new BaseFormPanel(new BaseStore<BaseObject>(), false);
		TableLayout requestLayout = new TableLayout(8);
		
		TableData layout1Times1 = new TableData();
		
		layout1Times1.setRowspan(1);
		layout1Times1.setHorizontalAlign(HorizontalAlignment.LEFT);
		layout1Times1.setVerticalAlign(VerticalAlignment.TOP);
		layout1Times1.setPadding(2);
		
		requestForm.setLayout(requestLayout);
		requestForm.setHeaderVisible(false);

		requestForm.add(new Label("申请人:"), layout1Times1);
		bname.setWidth(120);
		requestForm.add(bname, layout1Times1);
		
		requestForm.add(new Label("查询年度从:"), layout1Times1);
		startYear.setWidth(120);
		startYear.getPropertyEditor().setFormat(
				yearFormat);
		requestForm.add(startYear, layout1Times1);
		
		requestForm.add(new Label("到:"), layout1Times1);
		endYear.setWidth(120);
		endYear.getPropertyEditor().setFormat(
				yearFormat);
		requestForm.add(endYear, layout1Times1);
		
		requestForm.add(search, layout1Times1);
		requestForm.setWidth(810);
		requestForm.setHeight(50);
		requestForm.inited();

		return requestForm;
	}
	
	private Listener<BaseEvent> defaultlistener;

	private Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				String bnameRawValue = bname.getRawValue();
				String start = startYear.getRawValue();
				String end = endYear.getRawValue();
				
				StringBuilder sql = new StringBuilder();
				sql.append("select * from prolongYearVacationRequest where 1=1");
				
				if (bnameRawValue.equals("") || (start.equals("") && end.equals(""))) {
					MessageBox.alert("提示", "请输入查询条件", null);
					return;
				}
				
				if (!bnameRawValue.equals("")) {
					sql.append(" and bcode = '");
					sql.append(bname.getValueCode());
					sql.append("'");
				}
				if (!startYear.equals("")) {
					sql.append(" and year >= '");
					sql.append(start);
					sql.append("'");
				}
				if (!endYear.equals("")) {
					sql.append(" and year <= '");
					sql.append(end);
					sql.append("'");
				}
				
				requestStore.getStoreinfo().setSql(sql.toString());
				requestStore.reload();
			}

		};
		return defaultlistener;
	}
}
